package com.ling.ling_push.pusher;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import com.example.my_push.jni.PushNative;
import com.ling.ling_push.params.AudioParam;

class AudioPusher extends Pusher {
    private String TAG=this.getClass().getSimpleName();
    private AudioParam audioParam;
    private PushNative pushNative;
    private AudioRecord audioRecord;
    private int minBufferSize;
    private boolean isPushing = false;
    private int inputSamples;
    private int channelConfig;
    public AudioPusher(AudioParam audioParam, PushNative pushNative) {
        this.audioParam = audioParam;
        this.pushNative = pushNative;
        channelConfig = audioParam.getChannel() == 1 ? AudioFormat.CHANNEL_IN_MONO : AudioFormat.CHANNEL_IN_STEREO;
/*       if (audioParam.getChannel()==2){
           channelConfig=  AudioFormat.CHANNEL_IN_STEREO;
       }else {
           channelConfig=AudioFormat.CHANNEL_IN_MONO;
       }*/

       // inputSamples=pushNative.getInputSamples()*2;
      //  Log.i(TAG,"inputSamples:"+inputSamples);
        //最小缓冲区大小=采样个数*通道数// 采样频率  采样的声道个数  采用的位数（16位）
        minBufferSize = AudioRecord.getMinBufferSize(audioParam.getSampleRateInHz(), channelConfig, AudioFormat.ENCODING_PCM_16BIT)*audioParam.getChannel();
        audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
                audioParam.getSampleRateInHz(),
                channelConfig,
                AudioFormat.ENCODING_PCM_16BIT,
                //minBufferSize> inputSamples?inputSamples:minBufferSize
                minBufferSize
                );

    }

    @Override
    public void startPush() {
        isPushing = true;
        //启动一个录音子线程
         audioRecordTask =new AudioRecordTask();
         audioRecordTask.start();
    }

    @Override
    public void stopPush() {
        isPushing = false;
        audioRecord.stop();
    }

    @Override
    public void release() {
        if (audioRecord != null) {
            audioRecord.release();
            audioRecord = null;
        }
    }

    public void  AudioPause(){
        audioRecordTask.AudioRecordTaskPause();
    }
    private AudioRecordTask audioRecordTask;
    class AudioRecordTask extends Thread implements Runnable {
        private boolean IS_PUSHER_PCM=true;
        public void  AudioRecordTaskPause(){
            if (IS_PUSHER_PCM){
                IS_PUSHER_PCM=false;
            }else {
                IS_PUSHER_PCM=true;
            }
        }
        @Override
        public void run() {
            //开始录音
            audioRecord.startRecording();
            while (isPushing) {
                //通过AudioRecord不断读取音频数据
                byte[] buffer = new byte[minBufferSize];
                int len = audioRecord.read(buffer, 0, buffer.length);
                if (len > 0&&IS_PUSHER_PCM) {
                  //  Log.e("收到","音频数据:"+buffer.length);
                    //传给Native代码，进行音频编码
                    pushNative.fireAudio(buffer, len);
                }

            }
        }
    }
}
